package com.haoyun.mirage.auth.controller;

import com.google.code.kaptcha.Producer;
import com.google.common.collect.Sets;
import com.haoyun.common.constant.CommonConstant;
import com.haoyun.common.core.Result;
import com.haoyun.mirage.auth.client.UserServiceClient;
import com.haoyun.mirage.auth.service.IUserService;
import com.haoyun.mirage.auth.vo.UserVO;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.awt.image.BufferedImage;
import java.util.List;

/**
 * Created by twg on 2018/5/26.
 * 用户登录、注册、找回密码
 */
@RestController
@RequestMapping("/authentication")
public class AuthenticationController {

    @Autowired
    private Producer producer;

    @Autowired
    private IUserService userService;

    @Autowired
    private UserServiceClient userServiceClient;

    @Autowired
    private ConsumerTokenServices consumerTokenServices;

    @GetMapping("login")
    public ModelAndView login(HttpServletRequest request) {
        return new ModelAndView("login");
    }

    /**
     * 清除redis 中的token 信息
     *
     * @param accesstoken
     * @return
     */
    @PostMapping("removeToken")
    public Result<Boolean> removeToken(String accesstoken) {
        return Result.buildSuccessResult(consumerTokenServices.revokeToken(accesstoken));
    }

    /**
     * 注册用户
     *
     * @param user
     * @param bindingResult
     * @return
     */
    @GetMapping(value = "signup")
    public Result<String> signUp(@Valid UserVO user, BindingResult bindingResult) {
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            return Result.buildErrorResult(00001, fieldError.getDefaultMessage());
        }
        return userServiceClient.createUser(user);
    }

    @GetMapping("register")
    public ModelAndView register() {
        return new ModelAndView("register");
    }

    /**
     * 创建验证码
     *
     * @throws Exception
     */
    @GetMapping("code/{randomStr}")
    public void createCode(@PathVariable String randomStr, HttpServletResponse response)
            throws Exception {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        //生成文字验证码
        String text = producer.createText();
        //生成图片验证码
        BufferedImage image = producer.createImage(text);
        userService.saveCodeImage(randomStr, text);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "JPEG", out);
        IOUtils.closeQuietly(out);
    }

}
